<?php
namespace App\Modules\Relationship\Models;


use App\Http\Services\CommonServices;
use App\Modules\Relationship\Interfaces\ModelInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Base;

class SweetFriendReqModel extends Base {

    protected $table = "rs_sweet_friend_req";
    protected $fields = ['id', 'user_id as userId', 'user_title_id as userTitleId',
        'friend_id as friendId', 'friend_title_id as friendTitleId', 'expire_time as expireTime',
        'created_time as createdTime',
        'updated_time as updatedTime',
        'is_deleted as isDeleted'
        ];

    public function __construct(array $attributes = []){
        parent::__construct($this->table, $attributes);
        $this->tablePrefix = env('DB_PREFIX');
    }

    // 24小时内有没有请求过
    public function getReqWithin24hours($userId, $reqUserId) {
        $table = $this->tablePrefix . $this->table;
        $fields = implode(',', $this->fields);
        $timeInt = date("Y-m-d H:i:s",time()- 24 * 60 * 60);
        $sql = "select {$fields} from {$table} where user_id = ? and friend_id=? and expire_time > ? and is_deleted = 0";
        $bindings = [$userId, $reqUserId, $timeInt];
        $rows = DB::select($sql, $bindings);
        if (!$rows) {
            return null;
        }
        return $rows;
    }

    // 有没有请求过
    public function getRecord ($userId, $friendId) {

        $table = $this->tablePrefix . $this->table;
        $fields = implode(',', $this->fields);
        $timeInt = time() - 24 * 60 * 60;
        $sql = "select {$fields} from {$table} where user_id = ? and friend_id=? and created_time > ? and is_deleted = 0 order by id desc limit 1";
        $bindings = [$friendId, $userId, $timeInt];
        $rows = DB::select($sql, $bindings);
        if (!$rows) {
            return null;
        }
        $rows = self::objToArray($rows);
        return $rows[0];
    }
    public function getOneRecord ($userId, $friendId) {
        $condition =  array(
            'user_id'=>$userId,
            'req_user_id'=>$friendId,
            'is_deleted'=>0
        );
        return $this->getOneByCondition($condition, $this->fields);
    }

    public function getOneRecordById ($id) {
        $condition =  array(
            'id'=>$id,
            'is_deleted'=>0
        );
        return $this->getOneByCondition($condition, $this->fields);
    }

    public function getOneRecordByFriendId (int $userId, int $friendId) {
        $condition =  array(
            'user_id'=>$friendId,
            'friend_id'=>$userId,
            'is_deleted'=>0
        );
        return $this->getOneByCondition($condition, $this->fields);
    }

    public function insertSweetReq ($userId, $userTitleId, $friendId, $friendTitleId){
        $data = array(
            "user_id"=>$userId,
            "user_title_id"=>$userTitleId,
            "friend_id"=>$friendId,
            "friend_title_id"=>$friendTitleId,
            "expire_time"=>date('Y-m-d H:i:s',(time() + 24 * 60 * 60))
        );
        $re = DB::table($this->table)->insertGetId($data);
        return $re;
    }

    public function updateSweetReq ($id1, $id2, $state = 0, $isDeleted = 0){
        $data = array(
            "user_id"=>$id1,
            "friend_id"=>$id2,
            "state"=>$state,
            "is_deleted"=>$isDeleted
        );
        $re = DB::table($this->table)
            ->where('user_id', $id1)
            ->where('friend_id', $id2)
            ->update($data);
        return $re;
    }

    public function updateSweetReqById ($id, $state = 0, $isDeleted = 0){
        $data = array(
            "id"=>$id,
            "state"=>$state,
            "is_deleted"=>$isDeleted
        );
        $re = DB::table($this->table)
            ->where('id', $id)
            ->update($data);
        return $re;
    }

    public function deleteReq(int $id)
    {
        return DB::table($this->table)->where('id', $id)->update(array('is_deleted'=>1, 'state'=>3));
    }
}

